<?php
namespace app\myr\controller;
use think\admin\Controller;
use think\facade\Db;
use think\admin\helper\QueryHelper;
use app\myr\model\LotteryExpandMp as LotteryExpandMpModel;



/**
 * 抽奖管理奖项
 * Class Lottery
 */
class LotteryItem extends Controller
{
  
    /**
     * 列表
     * @auth true  # 表示需要验证权限
     * @menu true  # 添加系统菜单节点
     * @login true # 强制登录才可访问
     */
    public function index()
    {
       LotteryExpandMpModel::mQuery()->layTable(function () {
            $this->title = '抽奖管理';
        }, static function (QueryHelper $query) { $query->order("rank asc"); });
    }
    
    /**
     * 列表数据处理
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            if($vo["type"] == "3"){ $vo["type"]="红包";}
            else if($vo["type"] == "1"){ $vo["type"]="积分"; }
            else if($vo["type"] == "2"){ $vo["type"]="实物"; }
            if($vo["status"] == "1"){ $vo["status"]="显示";}
            else if($vo["status"] == "2"){ $vo["role_name"]="隐藏"; }
            $vo["currend_day"]=$this->get_currend_day($vo["id"]);
        }
    }
    
    function get_currend_day($id){
        $hongbao=Db::name("hongbao_date_log")->where("add_date",date('Y-m-d'))->where("lottery_id",$id)->find();
        if($hongbao){
            return $hongbao["send_num_count"];
        }else{
            return 0;
        }
    }
    
    /**
     * 新增抽奖
     */
    public function add(){
        
        LotteryExpandMpModel::mForm('form');
    }

    /**
     * 编辑抽奖
     */
    public function edit(){
         LotteryExpandMpModel::mForm('form');
    }


     //设置经营范围
    protected function _form_filter(array &$vo)
    {
        if ($this->request->isPost()) {
            $data=$this->request->post();
            if($this->request->action() == "add"){
              
            }else if($this->request->action() == "edit"){
               
            }
        }
        else{
            if(!isset($vo["type"])){ $vo["type"]=3; }
            if(!isset($vo["status"])){ $vo["status"]=1; }
        }
    }


    /**
     * 软删除抽奖
     */
    public function remove()
    {
        if ($this->request->isPost()) {
            $data=$this->request->post();
			$is=LotteryExpandMpModel::where("id",$data["id"])->find();
			if($is){
				LotteryExpandMpModel::where("id",$data["id"])->delete();
			}
		}
        LotteryExpandMpModel::mDelete();
    }
    /**
     * 抽奖积分设置
     */
    public function setting(){
        if ($this->request->isPost()){
            $data=$this->request->post();
            ($data['out_integral'] == '') && $this->error('新抽奖消耗积分不能为空');

            Db::connect($this->conn)->name($this->configtable)->where("id",1)->update($data);
            $this->success('保存数据成功','');
        }
        $data_list=Db::connect($this->conn)->name("lottery_config")->where("id",1)->field("out_integral,time")->find();
        $this->assign("out_integral",$data_list["out_integral"]);
        $this->assign("time",$data_list["time"]);
        return view('setting');
    }
}